﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Penult.BNet.Halo3.PlayedGames;

public partial class _Default : System.Web.UI.Page 
{
    Uri _gameUrl;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ShowGame()
    {
        Game game = new Game(_gameUrl);

        Response.Write(game.GameType.Name + " - " + game.Map.Name + "<br>");
        Response.Write(game.Link + "<br>");
        Response.Write("Playlist: " + game.Playlist.Name + "<br>");
        Response.Write(game.GameStart.ToString() + "<br><br><table width=800>");

        Response.Write("<tr><th>Place</th><th>Name</th><th>Kills</th><th>Deaths</th><th>Assists</th><th>Suicides</th><th>Betrayals</th><th>Score</th></tr>");

        if (game.Teams.Count != 0)
        {
            foreach (Team t in game.Teams)
            {
                WriteTeams(t);

                foreach (Player p in t.Players)
                {
                    WritePlayers(p);
                }
            }
        }
        else
        {
            foreach (Player p in game.Players)
            {
                WritePlayers(p);
            }
        }

        Response.Write("</table>");

        WebChart.ColumnChart ctlChart = new WebChart.ColumnChart();


        ctlChart.Fill.Color = System.Drawing.Color.FromArgb(50, System.Drawing.Color.SteelBlue);
        ctlChart.Line.Color = System.Drawing.Color.SteelBlue;
        ctlChart.Line.Width = 2;

        var players = from p in game.Players orderby p.Score select p;

        foreach (Player p in players)
        {
            ctlChart.Data.Add(new WebChart.ChartPoint(p.Name, p.Score));
        }

        ctlWebChart.Background.Color = System.Drawing.Color.DarkGray;
        ctlWebChart.Background.ForeColor = System.Drawing.Color.Yellow;
        ctlWebChart.Background.EndPoint = new System.Drawing.Point(500, 350);
        ctlWebChart.Legend.Position = WebChart.LegendPosition.Right;
        ctlWebChart.Legend.Width = 0;

        ctlWebChart.Width = 800;

        ctlWebChart.YAxisFont.ForeColor = System.Drawing.Color.Red;
        ctlWebChart.XAxisFont.ForeColor = System.Drawing.Color.Green;

        ctlWebChart.ChartTitle.Text = "Scores";
        ctlWebChart.ChartTitle.ForeColor = System.Drawing.Color.DarkBlue;

        ctlWebChart.Border.Color = System.Drawing.Color.SteelBlue;
        ctlWebChart.BorderStyle = BorderStyle.Ridge;

        ctlWebChart.Charts.Add(ctlChart);
        ctlWebChart.RedrawChart();
    }

    protected void WriteTeams(Team t)
    {
        string color = t.Name.Split(' ')[0];

        switch (color)
        {
            case "Red":
                {
                    color = "indianred";
                    break;
                }
            case "Brown":
                {
                    color = "coral";
                    break;
                }
            default:
                {
                    color = "light" + color;
                    break;
                }
        }

        Response.Write("<tr style=\"font-weight: bold; background-color: " + color + "\"><td>(" + t.Place + ")</td><td>" + t.Name +
            "</td><td>" + t.Kills +
            "</td><td>" + t.Deaths +
            "</td><td>" + t.Assists +
            "</td><td>" + t.Suicides +
            "</td><td>" + t.Betrayals +
            "</td><td>" + t.Score + "</td></tr>");
    }

    protected void WritePlayers(Player p)
    {
        string color = ((p.Team != null) ? p.Team.Name.Split(' ')[0] : "grey");

        switch (color)
        {
            case "Red":
                {
                    color = "indianred";
                    break;
                }
            case "Brown":
                {
                    color = "coral";
                    break;
                }
            default:
                {
                    color = "light" + color;
                    break;
                }
        }

        Response.Write("<tr style=\"background-color: " + color + "\"><td>(" + p.Place + ")</td><td>" + p.Name +
            "</td><td>" + p.Kills +
            "</td><td>" + p.Deaths +
            "</td><td>" + p.Assists +
            "</td><td>" + p.Suicides +
            "</td><td>" + p.Betrayals +
            "</td><td>" + p.Score + "</td></tr>");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        _gameUrl = new Uri(@"http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=105479793");

        ShowGame();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        _gameUrl = new Uri(@"http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=155897741");

        ShowGame();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        _gameUrl = new Uri(@"http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=156101833");

        ShowGame();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        _gameUrl = new Uri(@"http://www.bungie.net/Stats/GameStatsHalo3.aspx?gameid=164864656");

        ShowGame();
    }
}
